﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.ComponentModel;

namespace RandomReminder
{
    // TODO: Figure out what do do with Start button (grey / remove? / exit?)
    // TODO: Play a sound
    // TODO: Show an alert

    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private double _value = 0.0;
        private int _secondsStart = 0;
        private int _secondsLeft = 0;

        public double Value
        {
            get { return _value; }
            set { _value = value; OnPropertyChanged("Value"); }
        }

        private DispatcherTimer _timer;

        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            _secondsLeft -= 1;
            TimeText.Text = new TimeSpan(0, 0, _secondsLeft).ToString(@"m\:ss");
            if (_secondsLeft <= 60)
            {
                //TimeText.Style = (Style)FindResource("ImportantText");
                //TimeText.SetResourceReference(Control.StyleProperty, "ImportantText");
                TimeText.Foreground = new SolidColorBrush(Color.FromRgb(255, 0, 0));
            } 
            if (_secondsLeft == 0)
            {
                // DONE!
                _timer.Stop();
                Value = 100;
                // TODO: Do reminding / resetting / color fomatting? / pausing? / etc...
                System.Windows.MessageBox.Show("The timer has elapsed. Clock time will now be reset.","Remider");
                RestartTimer();
            }
            else
                Value = 100 - (double) 100 * _secondsLeft / _secondsStart;
        }

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        private void startButton_Click(object sender, RoutedEventArgs e)
        {
            RestartTimer();
        }

        private void RestartTimer()
        {
            if (_timer != null)
                _timer.Stop();

            Value = 0.0;
            var maxSeconds = Properties.Settings.Default.MaxMinutes * 60;
            var minSeconds = Properties.Settings.Default.MinMinutes * 60;
            if (minSeconds == maxSeconds)
                _secondsStart = minSeconds;
            else
                _secondsStart = new Random().Next(minSeconds, maxSeconds);
            _secondsLeft = _secondsStart;
            TimeText.Text = new TimeSpan(0, 0, _secondsLeft).ToString(@"m\:ss");
            TimeText.Foreground = new SolidColorBrush(Color.FromRgb(0, 0, 0));

            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromMilliseconds(1000);
            _timer.Tick += new EventHandler(Timer_Tick);
            _timer.Start();
        }
    }
}
